package com.gxan.web.renshi;

import com.alibaba.fastjson.JSONObject;
import com.gxan.pojo.Archives;
import com.gxan.renshiservice.ServiceArchives;
import com.gxan.renshiservice.impl.ServiceArchivesImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/archives/addArchives")
public class AddArchivesServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符编码
        resp.setContentType("application/json;charset=utf-8");
        //读取json数据
        String params = req.getReader().readLine();
        //
        Archives archives = JSONObject.parseObject(params, Archives.class);
        archives.setDept_id(Integer.valueOf(archives.getDept_full_name()));
        System.out.println();
        System.out.println("jobname"+archives.getJob_name());
        System.out.println("deptname"+archives.getDept_full_name());
        System.out.println("dpetid"+archives.getDept_id());
        System.out.println("jobid"+archives.getJob_id());
        System.out.println("date"+archives.getSet_time());
        System.out.println("status"+archives.getAudit_status());
        ServiceArchives serviceArchives = new ServiceArchivesImpl();
        int num = 0;
        Map map = new HashMap();
        try {
            num = serviceArchives.addArchives(archives);
            if (num>0){
                map.put("code",0);
                map.put("msg","添加成功");
                resp.getWriter().write(JSONObject.toJSONString(map));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","添加失败，系统错误");
            resp.getWriter().write(JSONObject.toJSONString(map));
        }
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
